load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("//bazel/go:platform.bzl", "platform_binary")
load("//bazel/patchelf:patchelf.bzl", "patchelf")

go_library(
    name = "bootstrapper_lib",
    srcs = [
        "main.go",
        "run.go",
        "test.go",
    ],
    importpath = "github.com/edgelesssys/constellation/v2/bootstrapper/cmd/bootstrapper",
    visibility = ["//visibility:private"],
    deps = [
        "//bootstrapper/internal/clean",
        "//bootstrapper/internal/diskencryption",
        "//bootstrapper/internal/initserver",
        "//bootstrapper/internal/joinclient",
        "//bootstrapper/internal/kubernetes",
        "//bootstrapper/internal/kubernetes/k8sapi",
        "//bootstrapper/internal/kubernetes/kubewaiter",
        "//bootstrapper/internal/nodelock",
        "//bootstrapper/internal/reboot",
        "//internal/atls",
        "//internal/attestation/choose",
        "//internal/attestation/initialize",
        "//internal/attestation/simulator",
        "//internal/attestation/tdx",
        "//internal/attestation/variant",
        "//internal/attestation/vtpm",
        "//internal/cloud/aws",
        "//internal/cloud/azure",
        "//internal/cloud/cloudprovider",
        "//internal/cloud/gcp",
        "//internal/cloud/metadata",
        "//internal/cloud/openstack",
        "//internal/cloud/qemu",
        "//internal/constants",
        "//internal/file",
        "//internal/grpc/dialer",
        "//internal/kubernetes/kubectl",
        "//internal/logger",
        "//internal/role",
        "//internal/versions/components",
        "@com_github_spf13_afero//:afero",
        "@io_k8s_kubernetes//cmd/kubeadm/app/apis/kubeadm/v1beta3",
    ],
)

go_binary(
    name = "bootstrapper",
    embed = [":bootstrapper_lib"],
    # keep
    gotags = select({
        "//bazel/settings:tpm_simulator_enabled": [],
        "//conditions:default": ["disable_tpm_simulator"],
    }),
    visibility = ["//visibility:public"],
)

platform_binary(
    name = "bootstrapper_linux_amd64",
    platform = "//bazel/platforms:constellation_os",
    target_file = ":bootstrapper",
)

patchelf(
    name = "bootstrapper_patched",
    src = ":bootstrapper_linux_amd64",
    out = "bootstrapper_with_nix_rpath",
    interpreter = "@cryptsetup_x86_64-linux//:dynamic-linker",
    rpath = "@cryptsetup_x86_64-linux//:rpath",
    visibility = ["//visibility:public"],
)

pkg_tar(
    name = "bootstrapper-package",
    srcs = [
        ":bootstrapper_patched",
    ],
    mode = "0755",
    remap_paths = {"/bootstrapper_with_nix_rpath": "/usr/bin/bootstrapper"},
    visibility = ["//visibility:public"],
)
